//
//  CJCollectionViewController.m
//  CoJellyCartoon
//
//  Created by dllo on 16/4/12.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "CJCollectionViewController.h"
#import "CoreDataManager.h"
#import "VideoInfo.h"
#import "CJCollectionTabelViewCell.h"
#import "CJPrePlayPageVIewController.h"
@interface CJCollectionViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CoreDataManager *coreDataManager;
@property (nonatomic, strong) NSMutableArray *coreDataArray;
@property (nonatomic, strong) UITableViewCell *Ecell;
@property (nonatomic, strong) UIBarButtonItem *clearAllBtn;

@end

@implementation CJCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"L20"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(Back)];
    
    self.coreDataManager = [CoreDataManager shareCoreDtaManager];
    self.coreDataArray = [NSMutableArray arrayWithCapacity:0];
    [self creatCollectionTableView];
    [self creatClearAllButton];
    
    
}


-(void)viewWillAppear:(BOOL)animated{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"VideoInfo" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    
    [fetchRequest setEntity:entity];
    
    NSArray *fetchedObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    [self.coreDataArray setArray:fetchedObjects];
    if (self.coreDataArray.count == 0) {
        UIImageView *HolderPic = [[UIImageView alloc] initWithFrame:self.view.bounds];
        HolderPic.image = [UIImage imageNamed:@"weishouchang"];
        self.tableView.tableHeaderView = HolderPic;
        
        self.tableView.scrollEnabled = NO;
    }
    
    [self.tableView reloadData];
    
    
}

-(void)creatCollectionTableView{
    self.tableView =[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
    [self.tableView registerClass:[CJCollectionTabelViewCell class] forCellReuseIdentifier:@"CJCollectionTabelViewCell"];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
    [self.view addSubview:self.tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.coreDataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CJCollectionTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJCollectionTabelViewCell"];
    cell.getVideoInfo = self.coreDataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.view.bounds.size.height / 6.5;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    
    VideoInfo *videoInfoModel = self.coreDataArray[indexPath.row];
    CJPrePlayPageVIewController *ToPlayVC = [[CJPrePlayPageVIewController alloc] init];
    ToPlayVC.idString = videoInfoModel.aid;
    ToPlayVC.picURL = videoInfoModel.pic;
        [self.navigationController pushViewController:ToPlayVC animated:YES];
    
}
#pragma mark-单个cell删除

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VideoInfo *videoInfo = self.coreDataArray[indexPath.row];
    
    [self.coreDataArray removeObjectAtIndex:indexPath.row];
    
    
    
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    
    [self.coreDataManager.managedObjectContext deleteObject:videoInfo];
    
    [self.coreDataManager saveContext];
    
    [self.tableView reloadData];
    
    if (self.coreDataArray.count == 0) {
        UIImageView *HolderPic = [[UIImageView alloc] initWithFrame:self.view.bounds];
        HolderPic.image = [UIImage imageNamed:@"weishouchang"];
        self.tableView.tableHeaderView = HolderPic;
        self.tableView.scrollEnabled = NO;
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

#pragma mark-全部删除
-(void)creatClearAllButton{
    self.clearAllBtn = [[UIBarButtonItem alloc] initWithTitle:@"全部清除" style:UIBarButtonItemStylePlain target:self action:@selector(ClearAll:)];
    self.clearAllBtn.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = self.clearAllBtn;
    
}
-(void)ClearAll:(UIBarButtonItem *)click{
    
    if (self.coreDataArray.count != 0) {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"VideoInfo" inManagedObjectContext:self.coreDataManager.managedObjectContext];
        
        [fetchRequest setEntity:entity];
        
        NSArray *fetchedObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
        [self.coreDataArray setArray:fetchedObjects];
        
        for (VideoInfo *videoInfo in self.coreDataArray) {
                [self.coreDataManager.managedObjectContext deleteObject:videoInfo];
        }
        
        [self.coreDataManager saveContext];
        [self.tableView reloadData];
        
        
            
            UIImageView *HolderPic = [[UIImageView alloc] initWithFrame:self.view.bounds];
            HolderPic.image = [UIImage imageNamed:@"weishouchang"];
            self.tableView.tableHeaderView = HolderPic;
            self.tableView.contentOffset = CGPointMake(0, 0);
            self.tableView.scrollEnabled = NO;
        [MBProgressHUD showSuccess:@"已删除所有收藏"];

    }else{
    
        [MBProgressHUD showSuccess:@"没有再多收藏让你删啦~~！"];
    }

    
}

-(void)Back{
    [self.navigationController popViewControllerAnimated:YES];
    
}
@end
